﻿namespace DeltaGroup.WheelOfJeopardy.UI.Screens
{
    using System.Windows.Controls;
    using System.Windows.Input;
    using DeltaGroup.WheelOfJeopardy.Util;
    using System.Linq;
    using System.Windows.Media;
    using System.Windows;
    using System.Diagnostics;

    /// <summary>
    /// Interaction logic for ChallengeEditor.xaml
    /// </summary>
    public partial class ChallengeEditor : UserControl
    {
        #region Constructors

        /// <summary>
        /// Initializes a new instance of the <see cref="ChallengeEditor"/> class.
        /// </summary>
        public ChallengeEditor()
        {
            InitializeComponent();
        }

        #endregion Constructors

        #region Methods

        /// <summary>
        /// Determines whether this instance can start the specified sender.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="System.Windows.Input.CanExecuteRoutedEventArgs"/> instance containing the event data.</param>
        private void CanSave(object sender, CanExecuteRoutedEventArgs e)
        {
            var jepCategories = from object item in jeopardyCategories.Items
                             let cp = jeopardyCategories.ItemContainerGenerator.ContainerFromItem(item) as ContentPresenter
                             let category = Extensions.FindVisualChild<TextBox>(cp)
                             where category != null
                             select category;

            var doubleJepcategories = from object item in doubleJeopardyCategories.Items
                                      let cp = doubleJeopardyCategories.ItemContainerGenerator.ContainerFromItem(item) as ContentPresenter
                                      let category = Extensions.FindVisualChild<TextBox>(cp)
                                      where category != null
                                      select category;

            e.CanExecute = jepCategories.Any(Validation.GetHasError) == false && doubleJepcategories.Any(Validation.GetHasError) == false;
            e.Handled = true;
        }

        #endregion Methods
    }
}